// pages/shenhe/shenheDetail.js
const app = getApp();
var userInfo;
const DB = wx.cloud.database()
var util = require('../../../utils/util.js');
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  user_status(){
    let user_status = this.data.userInfo.user_status?this.data.userInfo.user_status:false
    user_status = !user_status
    console.log('user_status',user_status)
    DB.collection('user').doc(wx.getStorageSync('seeuserInfo')._id).update({
      data: {
        user_status: user_status
      },
      success: function (res) {
        console.log('状态',res)
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })

        that.getUser()
      }
    })
  },
  tongguo(){
    this.up_examine(true);
  },
  no_tongguo(){
    this.up_examine(false);
  },
  // 审核，更新用户认证状态
  up_examine(status) {
      DB.collection('user').doc(wx.getStorageSync('seeuserInfo')._id).update({
        data: {
          examineStatus: status
        },
        success: function (res) {
          console.log('修改审核',res)
          wx.showToast({
            title: '操作成功',
            icon: 'none'
          })

          that.getUser()
          // that.senMsg(status,0)
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      })
  },
  senMsg(status,idx){
    console.log('打算发送消息')

    var data = {
      openid: getApp().guanli_list_openid[idx],
      page: "pages/my/index",
      date4: util.formatTime(new Date()),
      thing1: '认证审核',
      phrase2: "身份认证",
      name3: that.data.userInfo.userInfoDetail.nicheng,
      type:3,
      templateId: "bzyl7or1NmoyNJ_OEajl0I9WBpJ7idMLgqWjsjqS7RU",
      miniprogramState: "developer"
    }
    console.log('审核结果', (status?'审核通过':'审核未通过'))
    // developer为开发版；trial为体验版；formal为正式版；默认为正式版
    wx.cloud.callFunction({
      name: "sendMsg",
      data: data,
      success(res) {
        console.log('发送模板消息成功-------------', res.result)
        if(idx.length<getApp().guanli_list_openid.length-1){
          idx = idx+1
          that.senMsg(status,idx)
        }
      },
      fail(err) {
        console.log('发送模板消息失败-------------', err)

      }
    })
  },
  show_tip() {
    var show_tip = !this.data.show_tip
    this.setData({
      show_tip
    })
  },
  getUser(){
    DB.collection('user').doc(wx.getStorageSync('seeuserInfo')._id).get().then(rrr => {
      console.log('rrr',rrr)
      that.setData({
        userInfo:rrr.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    userInfo = wx.getStorageSync('seeuserInfo')
    that.getQuizList(userInfo.openid)
    this.getCharacterList()
    this.getUser()
  },

  // 获取三观列表
  getCharacterList() {
    DB.collection('character_list')
      .get({
        success: function (res) {
          let list = res.data
          console.log('list:', list)
          // list = getRandomArrayElements(list, 3)
          console.log('随机的list:', list)
          // list.forEach((value, i) => {
          // });
          that.setData({
            quest: list
          })
        },
        fail: function (err) {
          console.log('err', err)
        }
      })
  },
  // 获取问答列表
  getQuizList(openid) {
    DB.collection('quiz_list').where({
      b: openid
    }).get().then(res => {
      that.setData({
        Quiz: res.data
      })
      console.log(res.data)
    })
  },
})